package com.e.library.http;

import android.text.TextUtils;

import com.e.library.entity.User;
import com.e.library.utils.EGsonUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * json
 * Created by lxy on 2016/5/9.
 */
public class EResponse {

    public String json;
    public int status;
    public String message;
    public int page;
    public int page_limit;
    public String session;
    public JSONObject object;
    public JSONObject data;

    public EResponse(String json){
        this.json = json;
        try {
            object = new JSONObject(json);
            data = object.optJSONObject("data");
            message = object.optString("message","");
            status = object.optInt("status",-1);
            page = object.optInt("page_limit",0);

            if (data !=null){
                session = data.optString("session_id","");
                page = data.optInt("page",0);
                page_limit = data.optInt("page_limit",0);
            }

            if(!successful() && TextUtils.isEmpty(message)) {
                message = EClient.status.get(status);
            }

        } catch (JSONException e) {
            e.printStackTrace();
            message = EClient.status.get(-1);
            return;
        }
    }


    /**http响应成功*/
    public boolean successful(){
        return status == 200000;
    }

    public boolean toLogin(){
        return status == 100000;
    }

    /**登录过期*/
    public boolean expire(){
        return status == 400009;
    }

    public <T> T object(String key, Class<T> clazz){
        if (data == null){
            return null;
        }
        String value = data.optString(key,"");
        return EGsonUtils.toObject(value,clazz);
    }

    public User user(){
        return object("user",User.class);
    }

    /**
     * 获取默认key为list的集合
     * 如果不是默认key请使用另外一个函数{@link EResponse#list(String, Class)}
     * 请结合实际接口调用不同函数
     * */
    public <T> List<T> list(Class<T> clazz){
        return list("list",clazz);
    }

    public <T> List<T> list(String key, Class<T> clazz){
        if (data == null){
            return new ArrayList<>(0);
        }
        String value = data.optString(key,"");
        return EGsonUtils.toList(value,clazz);
    }

    public String string(String key){
        return string(key,"");
    }

    public String string(String key, String defValue){
        if (data == null){
            return defValue;
        }
        return data.optString(key,defValue);
    }

    public int integer(String key,int defValue){
        if (data == null){
            return defValue;
        }
        return data.optInt(key,defValue);
    }

    public double optDouble(String key,double defValue){
        if (data == null){
            return defValue;
        }
        return data.optDouble(key,defValue);
    }

    public long optLong(String key,long defValue){
        if (data == null){
            return defValue;
        }
        return data.optLong(key,defValue);
    }

    public boolean optBoolean(String key,boolean defValue){
        if (data == null){
            return defValue;
        }
        return data.optBoolean(key,defValue);
    }

    public int status() {
        return status;
    }

    public String message() {
        return message;
    }

    public String error(){
        return EClient.status.get(status);
    }

    public JSONObject data() {
        return data;
    }
}
